﻿using System;
using System.Collections.Generic;
using QFramework;
using UnityEngine;

namespace ProjectMyFarmAndShe
{
    /// <summary>
    ///     v0.2.2 完成任务系统功能小重构 摄像机跟随 日期更改方式改变 工具栏
    ///     V0.2.3 增加任务 任务完成后间隔一到两天再发布任务 完成系列任务并解锁新的系列任务 更改通关条件(完成所有系列任务通关) 完成任务提供奖励 并增加完成任务后能收到发布者的感谢信
    /// v0.3 重构工具栏保证原有功能不被破坏{切换工具实现功能} 添加物品进入工具栏{例如收获的种植物}
    ///     v0.3.1 完成任务系统和工具栏物品之间的交互{提交物品之后工具栏中物品减少}
    /// v0.4 商店系统 可以使用金币购买种子然后添加到工具栏
    /// v0.5 增加消息通知{购买商品、完成任务等行为之后会弹出消息框}
    ///     v0.5.1 重构资源加载
    ///     v0.5.2 打包测试之后Select工具图片显示不正确{材质球问题}
    /// v0.6 对话系统 通过对话选择打开商店购买或者贩卖
    ///     v0.6.1 贩卖物品界面以及数据显示
    ///
    /// 第一轮打磨
    /// v0.7 完善种植系统 增加植物种类 增加收割动画 增加工具使用效果{动画 音效 屏幕震动反馈} 收割时可以收获多个 植物在天数变更时存在死亡可能性 工具使用距离
    /// v0.8 完善工具栏选择提示 工具栏使用限制{下一天恢复体力 当前体力如果小于当前工具所需 那么工具变透明} 回信提示
    /// v0.9 增加层级 完成升级系统 升级提高体力获得金币 打开ui关闭select 切换鼠标icon 完成全部ui 完善商店{新增植物 每十天重置商店物品数据 会更新不同植物}
    /// 第二轮打磨
    /// v1.0 完善工具栏满了之后出现消息提示 完善商店数据更改出现的引用丢失 完善商店老板形象和字幕提示 家的形象完善 天数变更的音效 增加背景音乐 地图完善
    ///     v1.0.1 购买物品音效 出售物品音效 天数变更之后玩家位置发生改变 
    ///     v1.0.2 存档功能{工具栏存储完成 邮箱存储完成{邮件队列存储} 升级系统存储完成 挑战系列存储完成 商店物品存储完成 收获植物数量 女主名字 网格数据}
    /// 
    /// 最后完善 人物形象 完善任务剧情{例如任务描述 已经回信内容} 增加新增植物种类任务 层级显示
    ///
    /// 收尾
    /// </summary>
    public class Global : Architecture<Global>
    {
        public static Player Player;

        //天数
        public static BindableProperty<int> Day = new BindableProperty<int>(1);

        //当前工具
        public static ITool Tool = HarvestTool;
        public static EasyEvent<ITool> OnToolChange = new EasyEvent<ITool>();

        //金币
        public static BindableProperty<int> Coin = new BindableProperty<int>(100);

        //工具icon
        public static Select Mouse;

        //是否打开panel
        public static BindableProperty<bool> OpenPanel = new BindableProperty<bool>(false);

        //体力数据 后续体力升级直接升级当前变量
        public static int BrawnConfig = 40;

        //当前体力
        public static BindableProperty<float> Brawn = new BindableProperty<float>(BrawnConfig);

        #region 工具

        public static PotatoSeedTool PotatoSeedTool = new PotatoSeedTool();
        public static PinkRadishSeedTool PinkRadishSeedTool = new PinkRadishSeedTool();
        public static TurnipSeedTool TurnipSeedTool = new TurnipSeedTool();
        public static GrapeSeedTool GrapeSeedTool = new GrapeSeedTool();
        public static BlueBerrySeedTool BlueBerrySeedTool = new BlueBerrySeedTool();
        public static EggplantSeedTool EggplantSeedTool = new EggplantSeedTool();

        public static HarvestTool HarvestTool = new HarvestTool();
        public static HoeTool HoeTool = new HoeTool();
        public static ShowerTool ShowerTool = new ShowerTool();

        #endregion

        #region 收获植物数量

        public static BindableProperty<int> PotatoCount = new BindableProperty<int>(0);
        public static BindableProperty<int> TurnipCount = new BindableProperty<int>(0);
        public static BindableProperty<int> PinkRadishCount = new BindableProperty<int>(0);
        public static BindableProperty<int> GrapeCount = new BindableProperty<int>(0);
        public static BindableProperty<int> BlueBerryCount = new BindableProperty<int>(0);
        public static BindableProperty<int> EggplantCount = new BindableProperty<int>(0);

        #endregion

        #region 主线 女主名字

        public static BindableProperty<string> Heroine = new BindableProperty<string>("***");

        #endregion

        [RuntimeInitializeOnLoadMethod]
        static void AutoInit()
        {
            var _ = Global.Interface;
            ResKit.Init();
        }

        protected override void Init()
        {
            this.RegisterUtility<ISaveUtility>(new SaveUtility());
            
            this.RegisterSystem<IGridSystem>(new GridSystem());
            this.RegisterSystem<IChallengeSystem>(new ChallengeSystem());
            this.RegisterSystem<IEmailSystem>(new EmailSystem());
            this.RegisterSystem<IToolbarSystem>(new ToolbarSystem());
            this.RegisterSystem<IShopSystem>(new ShopSystem());
            this.RegisterSystem<ILevelUpSystem>(new LevelUpSystem());

            EventController.Default.AddEvent("OpenBuyShop", new EasyEvent());
            EventController.Default.AddEvent("OpenSellShop", new EasyEvent());

            var saveUtility = GetUtility<ISaveUtility>();

            Day.Value = saveUtility.LoadInt("Day", 1);
            Coin.Value = saveUtility.LoadInt("Coin", 100);
            Brawn.Value = saveUtility.LoadFloat("Brawn", BrawnConfig);

            PotatoCount.Value = saveUtility.LoadInt("PotatoCount", 25);
            TurnipCount.Value = saveUtility.LoadInt("TurnipCount", 25);
            PinkRadishCount.Value = saveUtility.LoadInt("PinkRadishCount", 0);
            EggplantCount.Value = saveUtility.LoadInt("EggplantCount", 0);
            GrapeCount.Value = saveUtility.LoadInt("GrapeCount", 0);
            BlueBerryCount.Value = saveUtility.LoadInt("BlueBerryCount", 0);

            Heroine.Value = saveUtility.LoadString("Heroine", "***");

            Day.RegisterWithInitValue(day => { saveUtility.SaveInt("Day", day); })
                .UnRegisterWhenGameObjectDestroyed(Player);

            Coin.RegisterWithInitValue(coin => { saveUtility.SaveInt("Coin", coin); })
                .UnRegisterWhenGameObjectDestroyed(Player);

            Brawn.RegisterWithInitValue(brawn => { saveUtility.SaveFloat("Brawn", brawn); })
                .UnRegisterWhenGameObjectDestroyed(Player);

            PotatoCount.RegisterWithInitValue(count => { saveUtility.SaveInt("PotatoCount", count); })
                .UnRegisterWhenGameObjectDestroyed(Player);

            TurnipCount.RegisterWithInitValue(count => { saveUtility.SaveInt("TurnipCount", count); })
                .UnRegisterWhenGameObjectDestroyed(Player);

            PinkRadishCount.RegisterWithInitValue(count => { saveUtility.SaveInt("PinkRadishCount", count); })
                .UnRegisterWhenGameObjectDestroyed(Player);

            EggplantCount.RegisterWithInitValue(count => { saveUtility.SaveInt("EggplantCount", count); })
                .UnRegisterWhenGameObjectDestroyed(Player);

            GrapeCount.RegisterWithInitValue(count => { saveUtility.SaveInt("GrapeCount", count); })
                .UnRegisterWhenGameObjectDestroyed(Player);

            BlueBerryCount.RegisterWithInitValue(count => { saveUtility.SaveInt("BlueBerryCount", count); })
                .UnRegisterWhenGameObjectDestroyed(Player);

            Heroine.RegisterWithInitValue(name => { saveUtility.SaveString("Heroine", name); })
                .UnRegisterWhenGameObjectDestroyed(Player);
        }

        /// <summary>
        /// 重置数据
        /// </summary>
        public static void ResetData()
        {
            Day.Value = 1;
        }
    }
}